package com.ruoyi.web.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysClassStudent;
import com.ruoyi.system.service.ISysClassService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 班级学员信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/user/course")
public class SysUserCourseController extends BaseController
{
    @Autowired
    private ISysClassService classService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询已分配该班级的学员列表
     */
    @PreAuthorize("@ss.hasPermi('system:userCourse:list')")
    @GetMapping("/assigned/{classId}")
    public TableDataInfo assignedList(@PathVariable("classId") Long classId)
    {
        startPage();
        List<SysUser> list = userService.selectAssignedList(classId);
        return getDataTable(list);
    }

    /**
     * 查询未分配该班级的学员列表
     */
    @PreAuthorize("@ss.hasPermi('system:userCourse:list')")
    @GetMapping("/unassigned/{classId}")
    public TableDataInfo unassignedList(@PathVariable("classId") Long classId)
    {
        startPage();
        List<SysUser> list = userService.selectUnassignedList(classId);
        return getDataTable(list);
    }

    /**
     * 批量选择学员加入班级
     */
    @PreAuthorize("@ss.hasPermi('system:userCourse:edit')")
    @Log(title = "学员课程管理", businessType = BusinessType.GRANT)
    @PostMapping("/addBatch/{userId}/{courseIds}")
    public AjaxResult addBatch(@PathVariable Long userId, @PathVariable Long[] courseIds)
    {
        return toAjax(classService.insertClassStudentAuth(userId, courseIds));
    }

    /**
     * 取消班级和学员关联
     */
    @PreAuthorize("@ss.hasPermi('system:userCourse:edit')")
    @Log(title = "学员课程管理", businessType = BusinessType.GRANT)
    @DeleteMapping("/cancel")
    public AjaxResult cancelAuthUser(SysClassStudent classStudent)
    {
        return toAjax(classService.deleteClassStudentInfo(classStudent));
    }

    /**
     * 批量取消班级和学员关联
     */
    @PreAuthorize("@ss.hasPermi('system:userCourse:edit')")
    @Log(title = "学员课程管理", businessType = BusinessType.GRANT)
    @DeleteMapping("/cancelAll")
    public AjaxResult cancelAll(Long classId)
    {
        return toAjax(classService.deleteClassStudentByClassId(classId));
    }
}